package com.leyou.auth.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.auth.entity.ClientInfo;
import com.leyou.auth.mapper.ClientAuthMapper;
import com.leyou.auth.service.ClientService;
import com.leyou.common.exception.LyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @author YYP
 * @date 2021/1/11
 */
@Service
public class ClientServiceImpl extends ServiceImpl<ClientAuthMapper, ClientInfo> implements ClientService   {

    //因为数据库中,secret字段是默认的加盐加密,所以我们需要注入passwordEncoder
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Value("${ly.jwt.key}")
    private String key;


    @Override
    public String getkey(String clientId, String secret) {
        ClientInfo clientInfo = this.query()
                .eq("client_id", clientId)
                .one();
        //如果传入的clientId有误,clientinfo查询为空,或者secret校验失败,都反悔403
        if (clientInfo == null || !passwordEncoder.matches(secret,clientInfo.getSecret())) {
            throw new LyException(403,"非法客户端请求");
        }



        return key;
    }
}
